package com.lk.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.lk.model.EventMessage;
import com.lk.model.Person;
import com.lk.model.User;
import com.lk.presenter.UserLoginPresenter;
import com.lk.view.ILoginView;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by liu kun on 2018/7/18 0018.
 */

public class TargetActivity extends Activity implements ILoginView {
    private Button post, sticky;
    private EditText nameEt, passEt;
    private Button loginBtn, clearBtn;
    private ProgressBar mProgressBar;
    private UserLoginPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.target_layout);
        initView();
        presenter=new UserLoginPresenter(this);
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person p = new Person();
                p.setAge(18);
                p.setName("赵飞燕");
                p.setSex(true);
                EventBus.getDefault().post(new EventMessage(p));
            }
        });
        sticky.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Person p = new Person();
                p.setAge(18);
                p.setName("赵飞燕");
                p.setSex(true);
                EventBus.getDefault().postSticky(new EventMessage(p));
                finish();
            }
        });
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.login();
            }
        });
        clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.clear();
            }
        });
    }

    private void initView() {
        post = (Button) findViewById(R.id.post);
        sticky = (Button) findViewById(R.id.stickyPost);
        nameEt = (EditText) findViewById(R.id.nameEt);
        passEt = (EditText) findViewById(R.id.passEt);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        clearBtn = (Button) findViewById(R.id.clearBtn);
        mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);
    }

    @Override
    public String getName() {
        return nameEt.getText().toString();
    }

    @Override
    public String getPassword() {
        return passEt.getText().toString();
    }

    @Override
    public void showLoading() {
        mProgressBar.setVisibility(View.VISIBLE);

    }

    @Override
    public void hideLoading() {
        mProgressBar.setVisibility(View.GONE);
    }

    @Override
    public void clearName() {
        nameEt.setText("");
    }

    @Override
    public void clearPassword() {
        passEt.setText("");
    }

    @Override
    public void toMainActivity(User user) {
        Toast.makeText(this, user.getName()+"Login Success !", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedError() {
        Toast.makeText(this, "Login Failed !", Toast.LENGTH_SHORT).show();
    }
}
